package com.leyou.auth.controller;

import com.leyou.auth.pojo.Cart;
import com.leyou.auth.pojo.Collections;
import com.leyou.auth.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 购物车Controller 哈哈
 */
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /*
        添加购物车
     */
    @PostMapping("addCart")
    public ResponseEntity<Void> addCart(@RequestBody(required = false) Cart cart) {
        try {
            cartService.addCart(cart);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /*
        查询购物车
     */
    @PostMapping("query")
    public ResponseEntity<Map<String, Object>> queryCarts(@RequestBody(required = false) Cart[] localCarts) {
        try {
            Map<String, Object> resultMap = this.cartService.queryCarts(localCarts);
            if (resultMap == null || resultMap.size() == 0) {
                ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /*
    修改商品数量
     */
    @PutMapping
    public ResponseEntity<Void> updateCarts(@RequestBody(required = false) Cart cart) {
        try {
            this.cartService.updateCarts(cart);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /*
        删除商品
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteCarts(@PathVariable("id") Long id) {
        try {
            this.cartService.deleteCarts(id);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //查询购物车长度
    @GetMapping("cartsLength")
    public ResponseEntity<Integer> queryCartsLength(@RequestParam("cartsLength") Integer cartsLength) {
        try {
            Integer length = this.cartService.queryCartsLength(cartsLength);
            if (length == 0) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
            }
            return ResponseEntity.ok(length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //根据skuId添加到我的收藏
    @PostMapping("{id}")
    public ResponseEntity<Void> addMyFavorite(@PathVariable("id") Long id) {
        try {
            Boolean result = this.cartService.addMyFavorite(id);
            if (result) {
                return ResponseEntity.status(HttpStatus.CREATED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //根据用户选中的商品添加到我的收藏
    @PostMapping("moveSelected")
    public ResponseEntity<Void> moveSelected(@RequestBody(required = false) Cart[] carts) {
        try {
            this.cartService.moveSelected(carts);
            return ResponseEntity.status(HttpStatus.CREATED).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    //查询我的关注
    @GetMapping("myFavorite")
    public ResponseEntity<List<Collections>> queryMyFavorite() {
        try {
            List<Collections> collections = this.cartService.queryMyFavorite();
            if (collections == null || collections.size() == 0) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
            return ResponseEntity.ok(collections);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //删除选中的商品信息
    @PostMapping("removeSelectedCarts")
    public ResponseEntity<Void> removeSelectedCarts(@RequestBody(required = false) Cart[] carts) {
        try {
            this.cartService.removeSelectedCarts(carts);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //删除购物车中的信息
    @PostMapping("deleteSelected")
    public ResponseEntity<Void> deleteSelected(@RequestBody(required = false) Cart[] carts) {
        try {
            this.cartService.removeSelectedCarts(carts);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //清除下架商品
    @PostMapping("clearOut")
    public ResponseEntity<Void> clearOut(@RequestBody(required = false) String[] invalids) {
        try {
            this.cartService.clearOut(invalids);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //移除单个关注商品
    @DeleteMapping("deleteFavorite/{id}")
    public ResponseEntity<Void> removeFavorite(@PathVariable("id") Long id) {
        try {
            Boolean result = this.cartService.removeFavorite(id);
            if (result) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            } else {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //移除所有关注商品
    @DeleteMapping("removeAllFavorite")
    public ResponseEntity<Void> removeAllFavorite() {
        try {
            Boolean result = this.cartService.removeAllFavorite();
            if (result) {
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            } else {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    //添加删除商品到我的关注
    @PostMapping("addDeleteToFavorite")
    public ResponseEntity<Void> addDeleteToFavorite(@RequestBody Collections collection) {
        try {
            Boolean result = this.cartService.addDeleteToFavorite(collection);
            if (result) {
                //204
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
